package model.component
{
	import flash.events.MouseEvent;
	
	import model.entity.Chapter;
	import model.entity.Option;
	
	import mx.collections.ArrayCollection;
	
	import spark.components.Button;
	import spark.components.List;
	import spark.components.supportClasses.SkinnableComponent;
	
	public class ChapterContainer extends SkinnableComponent
	{
		[SkinPart(required="true")] public var prevButton:Button;
		[SkinPart(required="true")] public var gotoButton:Button;
		[SkinPart(required="true")] public var zoominButton:Button;
		[SkinPart(required="true")] public var zoomoutButton:Button;
		[SkinPart(required="true")] public var optionsList:List;
		
		[Bindable]
		public var textSize:Number = 13;
		[Bindable]
		public var currentChapter:Number = 1;
		[Bindable]
		public var currentVisibleItem:Chapter;
		[Bindable]
		public var visitedPath:ArrayCollection = new ArrayCollection();
		
		protected var itemsToBePagedChanged:Boolean = false;
		protected var _itemsToBePaged:ArrayCollection;
		[Bindable]
		public function set itemsToBePaged(value:ArrayCollection):void
		{
			_itemsToBePaged = value;
			itemsToBePagedChanged = true;
			
			invalidateProperties();	
		}
		public function get itemsToBePaged():ArrayCollection
		{
			return _itemsToBePaged;
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if (itemsToBePagedChanged && _itemsToBePaged)
			{
				currentChapter = 1;
				visitedPath.removeAll();
				currentVisibleItem = getChapterPage();
				itemsToBePagedChanged = false;
			}
		}
		
		public function refreshBook():void
		{
			if(_itemsToBePaged)
			{
				currentChapter = 1;
				visitedPath.removeAll();
				currentVisibleItem = getChapterPage();
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (partName == "prevButton")
			{
				prevButton.addEventListener(MouseEvent.CLICK, handlePrevPage);
			}
			if (partName == "gotoButton")
			{
				gotoButton.addEventListener(MouseEvent.CLICK, handleChooseOption);
			}
			if (partName == "zoominButton")
			{
				zoominButton.addEventListener(MouseEvent.CLICK, handleZoomIn);
			}
			if (partName == "zoomoutButton")
			{
				zoomoutButton.addEventListener(MouseEvent.CLICK, handleZoomOut);
			}
		}
		
		private function handleZoomIn(e:MouseEvent):void
		{
			if(textSize < 20)
			{
				textSize++;
			}
		}
		
		private function handleZoomOut(e:MouseEvent):void
		{
			if(textSize > 5)
			{
				textSize--;
			}
		}
		
		private function handlePrevPage(e:MouseEvent):void
		{
			var lastItem:Number = visitedPath.length - 1;
			currentChapter = visitedPath.getItemAt(lastItem) as Number;
			visitedPath.removeItemAt(lastItem);
			currentVisibleItem = getChapterPage();
		}		
		private function handleChooseOption(e:MouseEvent):void
		{
			var currentOption:Option = new Option;
			visitedPath.addItem(currentChapter);
			currentOption = currentVisibleItem.options.getItemAt(optionsList.selectedIndex) as Option;
			currentChapter = currentOption.goto;
			currentVisibleItem = getChapterPage();
		}
		
		protected function getChapterPage():Chapter
		{
			var ChapterPage:Chapter = new Chapter();
			ChapterPage = _itemsToBePaged.getItemAt(currentChapter - 1) as Chapter;
			
			return ChapterPage;
		}
		
		
	}
}